//
//  ItemSettingViewController.m
//  SAYAKA_Table
//
//  Created by numanuma08 on 2013/07/21.
//  Copyright (c) 2013年 勘米良. All rights reserved.
//

#import "ItemSettingViewController.h"
#import "ItemEditViewProtcol.h"
#import "Item.h"
#import "ItemManager.h"
#import "AppDelegate.h"
#import "Category.h"

#define kSEGUE_ADD_ITEM @"segue_add_item"
#define kCELL_ITEM @"cell_item"

@interface ItemSettingViewController ()
@property(strong, nonatomic)NSDictionary* editButtons;
@property(strong, nonatomic)NSArray* items;
@end

@implementation ItemSettingViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    AppDelegate* appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
    NSManagedObjectContext* dbManager = appDelegate.managedObjectContext;
    _items = [ItemManager allRecord:dbManager];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    UIBarButtonItem* editButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:@selector(selectEdit:)];
    UIBarButtonItem* editEndButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(selectEditEnd:)];
    _editButtons = @{[NSNumber numberWithBool:YES]: editEndButton,
                     [NSNumber numberWithBool:NO]: editButton};
    
    UIBarButtonItem* addItemButtonn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(selectAdd:)];
    self.tabBarController.navigationItem.rightBarButtonItems = @[addItemButtonn, [_editButtons objectForKey:[NSNumber numberWithBool:self.isEditing]]];
}

- (void)viewWillDisappear:(BOOL)animated
{
    self.tabBarController.navigationItem.rightBarButtonItems = nil;
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    UINavigationController* nextView = (UINavigationController*)segue.destinationViewController;
    id<ItemEditViewProtcol> destination = (id<ItemEditViewProtcol>)nextView.topViewController;
    
    AppDelegate* appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
    NSManagedObjectContext* dbManager = appDelegate.managedObjectContext;
    
    destination.item  = ^(NSString* identifier){
        if ([identifier isEqualToString:kSEGUE_ADD_ITEM]) {
            return [ItemManager newRecord:dbManager];
        }
        return [ItemManager newRecord:dbManager];
    }(segue.identifier);
    
    destination.cancelCallback = ^(id<ItemEditViewProtcol> protcol){
        NSIndexPath* selectedIndexPath = self.tableView.indexPathForSelectedRow;
        [self.tableView deselectRowAtIndexPath:selectedIndexPath animated:YES];
    };
    destination.completeCallback = ^(id<ItemEditViewProtcol> procol,Item* item){
        _items = [ItemManager allRecord:dbManager];
        [self.tableView reloadData];
    };
}

- (IBAction)selectEdit:(id)sender
{
    UIBarButtonItem* addButton = [self.tabBarController.navigationItem.rightBarButtonItems objectAtIndex:0];
    BOOL nowState = self.isEditing;
    UIBarButtonItem* editButton = [_editButtons objectForKey:[NSNumber numberWithBool:!nowState]];
    self.tabBarController.navigationItem.rightBarButtonItems = @[addButton, editButton];
}

- (IBAction)selectEditEnd:(id)sender
{
    UIBarButtonItem* addButton = [self.tabBarController.navigationItem.rightBarButtonItems objectAtIndex:0];
    BOOL nowState = self.isEditing;
    UIBarButtonItem* editButton = [_editButtons objectForKey:[NSNumber numberWithBool:!nowState]];
    self.tabBarController.navigationItem.rightBarButtonItems = @[addButton, editButton];
}

- (IBAction)selectAdd:(id)sender
{
    [self performSegueWithIdentifier:kSEGUE_ADD_ITEM sender:self];
    [self setEditing:NO animated:YES];
}

-(void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    UIBarButtonItem* addButton = [self.tabBarController.navigationItem.rightBarButtonItems objectAtIndex:0];
    UIBarButtonItem* editButton = [_editButtons objectForKey:[NSNumber numberWithBool:editing]];
    self.tabBarController.navigationItem.rightBarButtonItems = @[addButton, editButton];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _items.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    Item* item = [_items objectAtIndex:indexPath.row];
    
    UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:kCELL_ITEM];
    cell.textLabel.text = item.name;
    cell.detailTextLabel.text = item.category1.name;
    return cell;
}
@end
